/*
 * @Description:
 * @Version: 1.0.0
 * @Autor: zhou
 * @Date: 2022-03-19 17:20:36
 * @LastEditors: zhou
 * @LastEditTime: 2022-06-29 20:30:46
 */
import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

import { login, logout, getInfo } from "@/api/login";
import { getToken, setToken, removeToken } from "@/utils/auth";
import { localPubEncrypt } from "@/utils/jsencrypt";
// import { getOne } from "@/api/company";
import Cookies from "js-cookie";

export default new Vuex.Store({
    namespaced: true,
    state: {
        title: "",
        screenW: 0,
        screenH: 0,
        handle_num: 0,
        epidemic_info: {},

		token: getToken(),
        name: "", // '铁山港管委'
        avatar: "", // '/static/images/profile.jpg'
        roles: [],
        permissions: [],
        userInfo: null,
        outCompany: false,
        company: {},
    },
    mutations: {
        SET_SCREENW: (state, screenW) => {
            state.screenW = screenW;
        },
        SET_SCREENH: (state, screenH) => {
            state.screenH = screenH;
        },
        SET_TITLE: (state, title) => {
            state.title = title;
        },
        SET_HANDLE_GLOBAL: (state, num) => {
            state.handle_num++;
        },
        SET_EPIDEMIC_INFO: (state, epi) => {
            state.epidemic_info = epi;
        },
		// 登录后的用户相关信息--------------
		SET_TOKEN: (state, token) => {
            state.token = token;
        },
        SET_NAME: (state, name) => {
            state.name = name;
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar;
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles;
        },
        SET_PERMISSIONS: (state, permissions) => {
            state.permissions = permissions;
        },
        SET_USERINFO: (state, userInfo) => {
            state.userInfo = userInfo;
            state.outCompany = Number(userInfo.userType) === 3;
        },
        SET_COMPANY: (state, company) => {
            state.company = company;
        },
    },
    actions: {
        setScreenW({ commit }, payload) {
            commit("SET_SCREENW", payload);
        },
        setScreenH({ commit }, payload) {
            commit("SET_SCREENH", payload);
        },
        setTitle({ commit }, payload) {
            commit("SET_TITLE", payload);
        },
        setEpidemic({ commit }, payload) {
            commit("SET_EPIDEMIC_INFO", payload);
        },
		// 登录
        Login({ commit }, userInfo) {
            const username = userInfo.username.trim();
            const password = localPubEncrypt(userInfo.password);
            // const password = userInfo.password
            const { code } = userInfo;
            const { uuid } = userInfo;
            return new Promise((resolve, reject) => {
                login(username, password, code, uuid)
                    .then((res) => {
                        setToken(res.data.token);
                        commit("SET_TOKEN", res.data.token);
                        resolve();
                    })
                    .catch((error) => {
                        reject(error);
                    });
            });
        },

        // 获取用户信息
        GetInfo({ commit, state }) {
            return new Promise((resolve, reject) => {
                getInfo()
                    .then((res) => {
                        const { user } = res.data;
                        // const avatar = user.avatar == "" ? require("@/assets/images/profile.jpg") : process.env.VUE_APP_BASE_API + user.avatar;
                        const avatar =
                            user.avatar == ""
                                ? "/static/images/profile.jpg"
                                : user.avatar;
                        if (res.data.roles && res.data.roles.length > 0) {
                            // 验证返回的roles是否是一个非空数组
                            commit("SET_ROLES", res.data.roles);
                            commit("SET_PERMISSIONS", res.data.permissions);
                        } else {
                            commit("SET_ROLES", ["ROLE_DEFAULT"]);
                        }
                        commit("SET_NAME", user.nickName);
                        Cookies.set('nick_name', user.nickName, {expires: 1})
                        commit("SET_AVATAR", avatar);
                        commit("SET_USERINFO", user);
                        resolve(res);
                        // 获取单位信息
                        // if (state.outCompany) {
                        //     getOne(user.companyId).then((res) => {
                        //         commit("SET_COMPANY", res.data);
                        //         console.log("res:", res);
                        //     });
                        // }
                    })
                    .catch((error) => {
                        reject(error);
                    });
            });
        },

        // 退出系统
        LogOut({ commit, state }) {
            return new Promise((resolve, reject) => {
                logout(state.token)
                    .then(() => {
                        commit("SET_TOKEN", "");
                        commit("SET_ROLES", []);
                        commit("SET_PERMISSIONS", []);
                        removeToken();
                        resolve();
                    })
                    .catch((error) => {
                        reject(error);
                    });
            });
        },

        // 前端 登出
        FedLogOut({ commit }) {
            return new Promise((resolve) => {
                commit("SET_TOKEN", "");
                removeToken();
                resolve();
            });
        },
    },
    modules: {},
});
